/*
 * Copyright (c) 2021, Texas Instruments Incorporated
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * *  Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * *  Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * *  Neither the name of Texas Instruments Incorporated nor the names of
 *    its contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "app.h"
#include "key/hw_key.h"
#include "lcd/lcd.h"
#include "led/led.h"
#include "page_manager.h"
#include "ti_msp_dl_config.h"

#include "app_distance_pid.h"
#include "app_speed_pid.h"

#include "motor/hw_encoder.h"
#include "motor/hw_timer.h"

#include "utils/debug_uart.h"
#include "page_ids.h"

int sys_time = 1;

volatile uint8_t key_scan_flag = 0;
volatile uint8_t led_toggle_flag = 0;
volatile uint8_t lcd_update_flag = 0;
volatile uint8_t encoder_flag = 0;

uint8_t g_motor_run_status = 0;
uint8_t g_motor_run_direction = 0;
uint32_t g_motor_run_speed = 50;

int main(void)
{

    SYSCFG_DL_init();

    debug_uart_init();

    // 初始化LED
    LED_Init();

    // 初始化LCD
    LCD_init();

    // 初始化按键
    key_init();

    // 初始化 SPEED PID
    speed_pid_init();

    // 初始化 DISTANCE PID
    distance_pid_init();

    // 初始化UI
    ui_init();

    encoder_init();

    while (1)
    {
        if (key_scan_flag)
        {
            key_scan();
            key_scan_flag = 0;
        }

        if (led_toggle_flag)
        {
            LED_Toggle();

            led_toggle_flag = 0;
        }

        if (lcd_update_flag)
        {
            lcd_update_flag = 0;
            PageManager_DispatchEvent(PAGE_EVENT_DATA_UPDATE, NULL);
        }

        if (encoder_flag)
        {
            encoder_flag = 0;
            if(PageManager_GetCurrentPageId() == PAGE_ID_SPEED) {
                encoder_update();
            }
            PageManager_DispatchEvent(PAGE_EVENT_DATA_UPDATE_MOTOR, NULL);
        }
    }
}

// 系统定时器中断处理函数，当前设置为 1ms 进入一次中断
void SysTick_Handler(void)
{
    sys_time++;

    if (sys_time % 20 == 0)
    {
        key_scan_flag = 1; // 设置按键扫描标志
    }

    if (sys_time % 20 == 0)
    {
        lcd_update_flag = 1; // 设置屏幕刷新标志
    }

    if (sys_time % 500 == 0)
    {
        led_toggle_flag = 1; // 设置LED切换标志
    }

    if (sys_time % 40 == 0)
    {
        encoder_flag = 1; // 设置ENCODER切换标志
    }
}